
#include "game.h"

using namespace std;

Game::Game() : tosave(false)
{
    //null;
}

Game::~Game()
{
    for(map<int, Mission*>::iterator it = quests.begin(); it != quests.end() ; ++it)
    {
        delete it->second;
    }
}

void Game::setPlayer(Player* p)
{
    // adds the player character to store
    playerChar = p;
}

void Game::readMissions(std::ifstream& inf)
{
    // reads the missions from a filestream
    inf >> missionCount;
    for (int i = 0; i < missionCount; ++i)
    {
        int id;
        int isPuzzle;
        int length;
        inf >> id >> isPuzzle >> length;
        string tmp = "_";
        getline(inf,tmp);
        if (isPuzzle == 0)
        {
            quests[id] = new Combat(playerChar, this, id);
        } else
        {
            quests[id] = new Puzzle(playerChar, this, id);
        }
        quests[id]->readText(length, inf);
        int i1; int i2;
        inf >> i1 >> i2;
        quests[id]->setNexts(i1, i2);
        if (isPuzzle == 0)
        {
            string name;
            int hp; int atk; int def; int speed; int exp;
            inf >> name >> hp >> atk >> def >> speed >> exp;
            ((Combat*)quests[id])->setMonster(name, hp, atk, def, speed, exp);
        } else
        {
            int ans; int margin; int exp;
            inf >> ans >> margin >> exp;
            ((Puzzle*)quests[id])->setData(ans, margin, exp);
        }
    }
}

void Game::nextMission(const int& x)
{
    // offers a save, if it isn't taken starts the next mission
    currentMission = x;
    if (tosave)
    {
        cout << "Do you wish to save your progress? (y for 'yes', anything else to skip)\t";
        char c;
        cin >> c;
        if (c == 'y' || c == 'Y')
        {
            cin.clear(); while(cin.peek() != '\n') cin.ignore();
            string fname;
            cout << "Give a name for the save:\t";
            cin >> fname;
            saveGame(fname);
            return;
        }
    }
    else
    {
        tosave = true;
    }
    cin.clear(); while(cin.peek() != '\n') cin.ignore();
    cout << quests[x]->getText() << endl;
    if (quests[x]->getPuzzle())
    {
        cout << "What is your answer?\t";
        int y;
        cin >> y;
        while (cin.fail())
        {
            cin.clear(); while (cin.peek() != '\n') cin.ignore();
            cout << endl << "That's not a number, try again:\t";
            cin >> y;
        }
        ((Puzzle*)quests[x])->answerTry(y);
    } else
    {
        ((Combat*)quests[x])->startCombat();
    }
}

void Game::saveGame(const std::string& saveName)
{
    // saves the game data to a given filestream
    string filename = saveName;
    filename.append(".save");
    ofstream saveF(filename.c_str());
    playerChar->saveStats(saveF);
    saveF << currentMission << endl << endl << missionCount << endl;
    for (map<int, Mission*>::iterator it = quests.begin(); it != quests.end(); ++it)
    {
        it->second->save(saveF);
        saveF << endl;
    }
    cout << "You'll be able to load the next time by providing the name '" << saveName << "'." << endl;
}

void Game::quitGame()
{
    // ends the game
    cout << "You lost the game!" << endl;
}
